//
//  RegisterViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/5.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//
#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIButton *zcBtn;
@property (strong, nonatomic) IBOutlet UILabel *phoneLabel;
@property (strong, nonatomic) IBOutlet UITextField *phoneTF;
@property (strong, nonatomic) IBOutlet UILabel *yzmLabel;
@property (strong, nonatomic) IBOutlet UITextField *yzmTF;
@property (strong, nonatomic) IBOutlet UILabel *yzmBtn;
@property (strong, nonatomic) IBOutlet UILabel *mimaLabel;
@property (strong, nonatomic) IBOutlet UITextField *mimaTF;

@end

@implementation RegisterViewController
{
    //发送验证码倒计时
    NSTimer *_djsTimer;
    NSInteger _daojishi;
}
- (IBAction)xieyiAction:(id)sender {
    
}
#pragma mark ---------- 注册
- (IBAction)registerAction:(id)sender {
    [self.view endEditing:YES];
    if ([self.phoneTF.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入手机号"];
    } else if ([self.yzmTF.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入验证码"];
    } else if ([self.mimaTF.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入密码"];
    } else {
        [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@/Login/register",HTTPLOAD] dictionary:@{@"v1":@"register",@"phone":self.phoneTF.text,@"password":self.mimaTF.text,@"code":self.yzmTF.text} HUD:YES block:^(id responseObject) {
            if ([responseObject[@"code"] isEqual:@200]) {
                [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"result"] forKey:@"U_id"];
                AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
                [app setMainRoot];
                [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
            } else {
                [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
            }
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.zcBtn.layer.cornerRadius = (WIDTH-60)*37/260/2;
    self.phoneTF.font = [UIFont systemFontOfSize:self.fontSize];
    self.phoneLabel.font = [UIFont systemFontOfSize:self.fontSize];
    self.mimaLabel.font = [UIFont systemFontOfSize:self.fontSize];
    self.mimaTF.font = [UIFont systemFontOfSize:self.fontSize];
    self.yzmTF.font = [UIFont systemFontOfSize:self.fontSize];
    self.yzmLabel.font = [UIFont systemFontOfSize:self.fontSize];
    
    UITapGestureRecognizer *yzmTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sendYZMAction:)];
    [self.yzmBtn addGestureRecognizer:yzmTap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ------------ 发送验证码
- (void)sendYZMAction:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
    if ([self.phoneTF.text isEqualToString:@""]) {
        [[SQShowMessage shareMessage] showMessageWithStr:@"请输入手机号"];
    } else {
        [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@/Sms/sendsms",HTTPLOAD] dictionary:@{@"v1":@"registersendsms",@"phone":self.phoneTF.text,@"star":@"register"} block:^(id responseObject) {
            if ([responseObject[@"code"] isEqual:@200]) {
                self.yzmBtn.backgroundColor = [UIColor colorWithRed:198.0/255.0 green:199.0/255.0 blue:205.0/255.0 alpha:1];
                self.yzmBtn.text = @"重新发送(60s)";
                _daojishi = 60;
                _djsTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(djsTimerAction:) userInfo:nil repeats:YES];
                self.yzmBtn.userInteractionEnabled = NO;
            } else {
                [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
            }
        }];
    }
}
#pragma mark ---------- textField代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.phoneTF) {
        [self.phoneTF resignFirstResponder];
        [self.yzmTF becomeFirstResponder];
    } else if (textField == self.yzmTF) {
        [self.yzmTF resignFirstResponder];
        [self.mimaTF becomeFirstResponder];
    } else {
        [self.view endEditing:YES];
    }
    return YES;
}
#pragma mark ---------- 发送验证码倒计时
- (void)djsTimerAction:(NSTimer *)timer
{
    _daojishi--;
    NSString *str = [NSString stringWithFormat:@"重新发送(%ld)",_daojishi];
    self.yzmBtn.text = str;
    if (_daojishi == 0) {
        [_djsTimer invalidate];
        self.yzmBtn.backgroundColor = Color;
        self.yzmBtn.userInteractionEnabled = YES;
        self.yzmBtn.text = @"发送验证码";
    }
}
#pragma mark ------------- 键盘弹出收起通知中心
- (void)keyBoardDidShow:(NSNotification *)noti
{
        [UIView animateWithDuration:0.2 animations:^{
            if (self.phoneTF.isFirstResponder) {
                self.view.center = CGPointMake(self.view.center.x, HEIGHT/2);
            } else if (self.yzmTF.isFirstResponder) {
                if (WIDTH < 330) {
                self.view.center = CGPointMake(self.view.center.x, HEIGHT/2-40);
                }
            } else {
                if (WIDTH < 330) {
                self.view.center = CGPointMake(self.view.center.x, HEIGHT/2-80);
                } else {
                    self.view.center = CGPointMake(self.view.center.x, HEIGHT/2-50);
                }
            }
        }];
}
- (void)keyBoardDidHide:(NSNotification *)noti
{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.center = CGPointMake(self.view.center.x, HEIGHT/2);
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
